package com.hzf.wxplatform.web.controller.manager.order;

import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.common.util.EhCacheUtil;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.order.dao.model.OrderUser;
import com.hzf.order.dao.model.OrderUserAddress;
import com.hzf.order.dao.model.OrderUserAddressExample;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.krb5.Config;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/12/8   10:36
 **/
@Controller
@RequestMapping("/orderUserAddress")
public class OrderUserAddressController extends BaseController {
    @Autowired
    ApiService apiService;
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    @ResponseBody
    public Object addOrder(Model model, OrderUser orderUser, HttpServletRequest request){
        System.out.println(orderUser.getUserName());

        return  null;
    }
    @RequestMapping(value="/addressList")
    public String addressList(Model model,HttpServletRequest request,String orderType){
        byte [] a = UcenterUserUtils.getUser(request.getSession());
        System.out.println(a);
        UcenterUser ucenterUser = (UcenterUser) ByteUtils.ByteToObject(a);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        OrderUserAddressExample example = new OrderUserAddressExample();
        example.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        List<OrderUserAddress> list= apiService.selectExample(example);
        if(null!=list){
        for(OrderUserAddress address:list){
            ConfigRegionExample configRegionExample = new ConfigRegionExample();
            configRegionExample.createCriteria().andRegionIdEqualTo(address.getConfigId());
            ConfigRegion configRegion = apiService.selectAddressRegion(configRegionExample);
            address.setConfigName(configRegion.getRegionName());
        }}
        model.addAttribute("orderType",orderType);
        model.addAttribute("result",list);
        return  thymeleaf("/YZ/YZ_GWC_SelectAdress");
    }
    @RequestMapping(value = "/addAddress")
    public String addAddress(Model model,HttpServletRequest request,String type){
        byte [] a = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = (UcenterUser) ByteUtils.ByteToObject(a); if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        byte [] b =UcenterUserUtils.getAddress(ucenterUser.getId());
        ConfigRegion region = (ConfigRegion) ByteUtils.ByteToObject(b);
        model.addAttribute("configRegion",region);
        model.addAttribute("orderType",type);
        return  thymeleaf("/YZ/YZ_GWC_ADDAdress");

    }
    @RequestMapping(value = "/insertAddress",method = RequestMethod.POST)
    @ResponseBody
    public Object insertAddress(OrderUserAddress orderUserAddress,String orderType,Model model,HttpServletRequest request){
        byte [] a = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = (UcenterUser) ByteUtils.ByteToObject(a); if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        orderUserAddress.setUserId(ucenterUser.getId());
        orderUserAddress=apiService.insertOrderUserAddress(orderUserAddress);
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, orderType);
    }
    @RequestMapping(value = "/getAddress",method = RequestMethod.POST)
    @ResponseBody
    public Object getAddress(Model model,HttpServletRequest request,int id,String type){
        byte [] a = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = (UcenterUser) ByteUtils.ByteToObject(a); if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        OrderUserAddressExample orderUserAddressExample = new OrderUserAddressExample();
        orderUserAddressExample.createCriteria().andIdEqualTo(id).andUserIdEqualTo(ucenterUser.getId());
        List<OrderUserAddress> list = apiService.selectExample(orderUserAddressExample);
        OrderUserAddress orderUserAddress = list.get(0);
        EhCacheUtil.remove("hzf-wxplatform-web-ehcache",ucenterUser.getId()+"dizhi");
        EhCacheUtil.put("hzf-wxplatform-web-ehcache",ucenterUser.getId()+"dizhi", orderUserAddress);
        orderUserAddress=(OrderUserAddress) EhCacheUtil.get("hzf-wxplatform-web-ehcache",ucenterUser.getId()+"dizhi");
        String ids =null;
        if(type.equals("0")){
             ids=(String)EhCacheUtil.get("hzf-wxplatform-web-ehcache",ucenterUser.getId()+"dingdan");

        }else if(type.equals("3")){
             ids="3";
        }else{
             ids="0";

        }

        return new WxplatformResult(WxplatformResultConstant.SUCCESS, ids);

    }
}
